package com.elock.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.dao.impl.DaoCenter;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.DateUtil;
import com.elock.base.core.util.WebUtils;

/**
 * Dao - 终端消息信息
 * @author 仁德
 */
@Repository("terminalNotifyMessageDao")
public class TerminalNotifyMessageDao extends DaoCenter {

	public Page<Map<String, Object>> findPage(Pageable pageable, Object[] args) {

		List<Object> list = new ArrayList<Object>();
		Long companyInfoId = WebUtils.getCurrentCompanyInfoId();
		String terminalNum = (String) args[0];
		String elockNum = (String) args[1];
		Integer type = (Integer) args[2];
		Integer isProcess = (Integer) args[3];
		String taskSn = (String) args[4];
		String carNumber = (String) args[5];

		StringBuilder sql = new StringBuilder();
		sql.append("select nm.*,wm.warning_message,ec.car_number,ec.berth");
		sql.append(" from terminal_notify_message nm");
		sql.append(" left join warning_message wm on wm.uuid = nm.uuid");
		sql.append(" left join elock_car ec on ec.elock_num = nm.elock_num");
		sql.append(" where nm.company_info_id = ?");
		list.add(companyInfoId);

		if (!ConvertUtil.isEmpty(terminalNum)) {
			sql.append(" and nm.terminal_num like ?");
			list.add("%" + terminalNum + "%");
		}
		if (!ConvertUtil.isEmpty(elockNum)) {
			sql.append(" and nm.elock_num like ?");
			list.add("%" + elockNum + "%");
		}
		if (type != null) {
			sql.append(" and nm.type = ?");
			list.add(type);
		}
		if (isProcess != null) {
			sql.append(" and nm.is_process = ?");
			list.add(isProcess);
		}
		if (!ConvertUtil.isEmpty(taskSn)) {
			sql.append(" and nm.task_sn like ?");
			list.add("%" + taskSn + "%");
		}
		if (!ConvertUtil.isEmpty(carNumber)) {
			sql.append(" and ec.car_number like ?");
			list.add("%" + carNumber + "%");
		}
		sql.append(" order by nm.modify_date desc");

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		Page<Map<String, Object>> page = getNativeDao().findPageMap(sql.toString(),
				objs,
				pageable);

		return page;
	}

	/**
	 * 获取未处理消息通知数
	 */
	public int getNoProcess() {

		String sql = "select count(1) from terminal_notify_message where is_process = 0 and company_info_id=?";
		int count = getNativeDao().findInt(sql,
				new Object[] { WebUtils.getCurrentCompanyInfoId() });

		return count;
	}

	/**
	 * 更新消息通知处理结果
	 */
	public void updateProcessResult(Long[] ids, String processResult, String processor,
			Date searchTime) {

		String idsStr = "";
		if (ids != null && ids.length > 0) {
			for (Long id : ids) {
				idsStr += "," + id;
			}
		}
		if (idsStr.length() > 0) {
			idsStr = idsStr.substring(1);
		}

		List<Object> list = new ArrayList<Object>();

		StringBuilder sql = new StringBuilder();
		sql.append("update terminal_notify_message set is_process=1,");
		sql.append(" process_result=?,processor=?,process_time=?");
		sql.append(" where is_process=0");
		if (searchTime == null) {
			Long companyInfoId = WebUtils.getCurrentCompanyInfoId();
			sql.append(" and company_info_id=?");
			list.add(companyInfoId);
		}
		list.add(processResult);
		list.add(processor);
		list.add(DateUtil.convert(new Date()));

		if (idsStr.length() > 0) {
			sql.append(" and id in (" + idsStr + ")");
		}
		if (searchTime != null) {
			sql.append(" and modify_date<?");
			list.add(searchTime);
		}

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		getNativeDao().update(sql.toString(), objs);
	}
}